/**
 * Description:
 * Class Name:subject1_use_synchronized_method
 * Date:2022/5/19 10:41
 *
 * @author LTisme
 * @version JAVA17
 */
package 疯狂Java讲义.第16章_多线程.课后练习;

class SynchronizedPrinter{
    private int index = 1;

    public synchronized void printNum(){
        for (int i = 1; i <= 52 ; i++) {
            try {
                while (index % 3 == 0){
                    wait();
                }
                System.out.print(i);
                index++;
                notifyAll();
            }
            catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    public synchronized void printChar(){
        for (char ch = 'A'; ch <= 'Z'; ch++){
            try {
                while (index % 3 != 0){
                    wait();
                }
                System.out.print(ch);
                index++;
                notifyAll();
            }
            catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}

public class subject1_use_synchronized_method {
    public static void main(String[] args) {
        var obj = new SynchronizedPrinter();
        new Thread(obj::printNum).start();
        new Thread(obj::printChar).start();
    }
}
